<?php

namespace app\service;

use app\model\TaskModel;
use app\tool\SingletonTrait;
use think\db\Query;

class TaskStatisticsDynamicService
{
    use SingletonTrait;

    /**
     * 计划内逾期
     * @param array $param
     * @return Query
     */
    public static function taskInPlanOverdueBaseQuery(array $param): Query
    {
        $dbr = TaskModel::whereIn('task.project_id', $param['project_id']);
        switch ($param['history']) {
            case "solid":
            case "yes":
            default:
                $dbr = $dbr->whereRaw('(task.plan_end_time between ' . $param['start_time'] . ' and ' . $param['end_time']
                    . ' and (task.end_time not between 1 and task.plan_end_time '
                    . 'or get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'ready_to_publish', 'final'], false, 'id') . ')'
                    . '))'
//                    . ' or (task.plan_delivery_time between ' . $param['start_time'] . ' and ' . $param['end_time']
//                    . ' and (task.delivery_time not between 1 and task.delivery_time '
//                    . 'or get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'final'], false, 'id') . ')'
//                    . '))'
                )
                    ->whereFieldRaw('get_json_string(task.`json`, "$.task_status")', '<>', StatusService::getInstance()->byCode('closed', 'id'));
                break;
        }
        return $dbr;
    }


    /**
     * 未处理
     * @param array $param
     * @return Query
     */
    public static function taskInPlanNotDealBaseQuery(array $param): Query
    {
        $dbr = TaskModel::whereIn('task.project_id', $param['project_id'])
            ->join(['delay_order_task' => 'dot'], 'dot.task_id = task.id', 'left');
        switch ($param['history']) {
            case "solid":
            case "yes":
            default:
                $dbr = $dbr->whereFieldRaw('get_json_string(task.`json`, "$.task_status")', '<>', StatusService::getInstance()->byCode('closed', 'id'))
                    ->whereRaw('(task.plan_end_time between ' . $param['start_time'] . ' and ' . $param['end_time']
                        . ' and (task.end_time not between 1 and task.plan_end_time '
                        . 'or get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'ready_to_publish', 'final'], false, 'id') . ')'
                        . '))')
                    ->whereRaw('dot.type is null or dot.type <> "today_done" or dot.original_plan_time < task.plan_end_time')
                    ->whereRaw('dot.manager_confirmed is null or dot.manager_confirmed <> "yes" or dot.delay_time < task.plan_end_time');
                break;
        }
        return $dbr;
    }

    /**
     * 处理中
     * @param array $param
     * @return Query
     */
    public static function taskInPlanInProgressBaseQuery(array $param): Query
    {
        $dbr = TaskModel::whereIn('task.project_id', $param['project_id'])
            ->join(['delay_order_task' => 'dot'], 'dot.task_id = task.id', 'left');
        switch ($param['history']) {
            case "solid":
            case "yes":
            default:
                $dbr = $dbr->whereRaw('get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'ready_to_publish', 'final'], false, 'id') . ')')
                    ->whereRaw('(task.plan_end_time between ' . $param['start_time'] . ' and ' . $param['end_time']
                        . ' and (task.end_time not between 1 and task.plan_end_time '
                        . 'or get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'ready_to_publish', 'final'], false, 'id') . ')'
                        . '))')
                    ->whereIn('dot.type', ['request_delay', 'work_order'])
                    ->where('dot.manager_confirmed', 'default')
                    ->whereRaw('dot.original_plan_time = task.plan_end_time');
                break;
        }
        return $dbr;
    }

    /**
     * 已申请延期
     * @param array $param
     * @return Query
     */
    public static function taskInPlanMarkedDelayBaseQuery(array $param): Query
    {
        $dbr = TaskModel::whereIn('task.project_id', $param['project_id'])
            ->join(['delay_order_task' => 'dot'], 'dot.task_id = task.id', 'left');
        switch ($param['history']) {
            case "solid":
            case "yes":
            default:
                $dbr = $dbr->whereFieldRaw('get_json_string(task.`json`, "$.task_status")', '<>', StatusService::getInstance()->byCode('closed', 'id'))
                    ->whereRaw('(task.plan_end_time between ' . $param['start_time'] . ' and ' . $param['end_time']
                        . ' and (task.end_time not between 1 and task.plan_end_time '
                        . 'or get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'ready_to_publish', 'final'], false, 'id') . ')'
                        . '))')
                    ->whereIn('dot.type', ['request_delay', 'work_order'])
                    ->where('dot.manager_confirmed', 'yes')
                    ->whereRaw('dot.delay_time >= task.plan_end_time');
                break;
        }
        return $dbr;
    }

    /**
     * 已标记今日完成
     * @param array $param
     * @return Query
     */
    public static function taskInPlanMarkedTodayBaseQuery(array $param): Query
    {
        $dbr = TaskModel::whereIn('task.project_id', $param['project_id'])
            ->join(['delay_order_task' => 'dot'], 'dot.task_id = task.id', 'left');
        switch ($param['history']) {
            case "solid":
            case "yes":
            default:
                $dbr = $dbr->whereFieldRaw('get_json_string(task.`json`, "$.task_status")', '<>', StatusService::getInstance()->byCode('closed', 'id'))
                    ->whereRaw('(task.plan_end_time between ' . $param['start_time'] . ' and ' . $param['end_time']
                        . ' and (task.end_time not between 1 and task.plan_end_time '
                        . 'or get_json_string(task.`json`, "$.task_status") not in (' . StatusService::getInstance()->byCodeMap(['closed', 'ready_to_publish', 'final'], false, 'id') . ')'
                        . '))')
                    ->where('dot.type', 'today_done')
                    ->whereRaw('dot.original_plan_time >= task.plan_end_time');
                break;
        }
        return $dbr;
    }

    /**
     * 工单待办任务
     * @param array $param
     * @return mixed
     */
    public static function taskWorkOrderToDoBaseQuery(array $param)
    {
        $dbr = TaskModel::whereIn('task.project_id', $param['project_id'])
            ->join('task_work_order_task_list', 'task_work_order_task_list.task_id = task.id', 'left')
            ->join('task_work_order', 'task_work_order.id = task_work_order_task_list.work_order_id', 'left')
            ->whereBetween('task_work_order.created', [$param['start_time'], $param['end_time']]);
        return $dbr;
    }
}
